/*
Simple Forum Auto Poster.
Copyright (C) 2011 Gianni Van Hoecke.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package gui.posting;

import domain.DomainController;
import domain.forumdata.Category;
import domain.forumdata.Post;
import domain.forumdata.tag.Tag;
import domain.forumdata.tag.Tags;
import java.util.logging.Level;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

/**
 *
 * @author Gianni Van Hoecke <gianni.vh@gmail.com>
 */
public class PostingPanel extends javax.swing.JPanel
{

    /** Creates new form PostingPanel */
    public PostingPanel()
    {
        initComponents();
    }

    public void loadCategories(DefaultComboBoxModel model)
    {
        this.postParameterPanel1.loadCategories(model);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        postCommandsPanel1 = new gui.posting.PostCommandsPanel();
        postParameterPanel1 = new gui.posting.PostParameterPanel();
        postBodyPanel1 = new gui.posting.PostBodyPanel();

        setLayout(new java.awt.BorderLayout());
        add(postCommandsPanel1, java.awt.BorderLayout.PAGE_END);
        add(postParameterPanel1, java.awt.BorderLayout.PAGE_START);
        add(postBodyPanel1, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private gui.posting.PostBodyPanel postBodyPanel1;
    private gui.posting.PostCommandsPanel postCommandsPanel1;
    private gui.posting.PostParameterPanel postParameterPanel1;
    // End of variables declaration//GEN-END:variables

    public void postNewTopic()
    {
        String category = (String)this.postParameterPanel1.getCmbCategory().getSelectedItem();
        String topicTitle = this.postParameterPanel1.getTxtTitle().getText();
        String topicSubtitle = this.postParameterPanel1.getTxtSubtitle().getText();
        String tag = (String)this.postParameterPanel1.getCmbTags().getSelectedItem();
        String post = this.postBodyPanel1.getText();
        String[] selectedForums = DomainController.getInstance().getSelectedForums();
        String err = "";

        if(DomainController.getInstance().isCurrentPostGoodToPost())
        {
            if(selectedForums.length == 0)
            {
                err = "Please select one or more forums to post to.";
                DomainController.getInstance().getLogger().log(Level.WARNING, err);
                DomainController.getInstance().fireMessage(err, "Error");
            }
            else
            {
                DomainController.getInstance().startPosting(category, topicTitle, topicSubtitle, tag, post, selectedForums);
            }
        }
    }

    public void previewNewTopic()
    {
        final String post = this.postBodyPanel1.getText();
        if(post.length() > 0)
        {
            SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    PostPreviewFrame ppf = new PostPreviewFrame(post);
                    ppf.setLocationRelativeTo(null);
                    ppf.setVisible(true);
                }
            });
        }
        else
        {
            JOptionPane.showMessageDialog(this, "Nothing to preview.", "Preview", JOptionPane.INFORMATION_MESSAGE);
        }
    }

    public void setButtonsEnabled(boolean enabled)
    {
        this.postParameterPanel1.setButtonsEnabled(enabled);
        this.postCommandsPanel1.setButtonsEnabled(enabled);
        this.postBodyPanel1.setButtonsEnabled(enabled);
    }

    public void clearPost()
    {
        this.postParameterPanel1.clearPost();
        this.postBodyPanel1.clearPost();
    }

    public void loadPost(Post post)
    {
        this.postBodyPanel1.clearPost();
        this.postBodyPanel1.setText(post.getPost());
        this.postBodyPanel1.setCaretPosition(0);
        this.postParameterPanel1.getCmbCategory().setSelectedItem(post.getCat().getName());
        this.postParameterPanel1.getTxtTitle().setText(post.getTopicTitle());
        this.postParameterPanel1.getTxtSubtitle().setText(post.getTopicSubtitle());
        this.postParameterPanel1.getCmbTags().setSelectedItem(post.getTag().getTagName());
    }

    public void setPostBodyTo(String body)
    {
        this.postBodyPanel1.clearPost();
        this.postBodyPanel1.setText(body);
        this.postBodyPanel1.setCaretPosition(0);
    }

    public String getPost()
    {
        return this.postBodyPanel1.getText();
    }

    public void insertTagInPostBody(String tag)
    {
        this.postBodyPanel1.insertTagInPostBody(tag);
    }

    public String getSelectedTextInPost()
    {
        return this.postBodyPanel1.getSelectedTextInPost();
    }

    public Post getCurrentPost()
    {
        String category = (String)this.postParameterPanel1.getCmbCategory().getSelectedItem();
        String topicTitle = this.postParameterPanel1.getTxtTitle().getText();
        String topicSubtitle = this.postParameterPanel1.getTxtSubtitle().getText();
        String tag = (String)this.postParameterPanel1.getCmbTags().getSelectedItem();
        String post = this.postBodyPanel1.getText();

        Tag theTag = null;
        for(Tag t : Tags.getTags())
        {
            if(t.getTagName().equals(tag))
            {
                theTag = t;
                break;
            }
        }
        
        return new Post(new Category(category), topicTitle, topicSubtitle, theTag, post);
    }
}